/**
 * @Author: 焦质晔
 * @Date: 2019/6/20
 * @Last Modified by: 焦质晔
 * @Last Modified time: 2019-11-26 09:26:55
 */
import _ from 'lodash';
import { notifyAction } from '@/utils';
import store from '@/store';

const getLocalDict = () => {
  return JSON.parse(localStorage.getItem('dict')) || {};
};
const getLocalRegion = () => {
  return JSON.parse(localStorage.getItem('region')) || {};
};
const getLocalAction = () => {
  return JSON.parse(localStorage.getItem('action')) || {};
};
//店端值列表
const getDlrLocalDict = () => {
  return JSON.parse(localStorage.getItem('dlrDict')) || {};
};

export const dictionary = {
  beforeCreate() {
    this.dict = getLocalDict() || store.state.app.dict;
    this.region = getLocalRegion();
    this.action = getLocalAction();
    if (!Object.keys(this.dict).length) {
      notifyAction('本地数据字典被清空，请刷新当前页面!', 'warning');
      store.dispatch('app/createDictData');
    }
    this.dlrDict = getDlrLocalDict();
  },
  methods: {
    // code -> 数据字典的 code 码
    // vals -> 需要过滤数据字典项的值
    createDlrDictList(code, vals = []) {
      vals = Array.isArray(vals) ? vals : [vals];
      let res = [];
      if (_.isObject(this.dlrDict) && Array.isArray(this.dlrDict[code])) {
        res = this.dlrDict[code].map(x => ({
          text: x.codeCnDesc,
          value: x.codeId
        }));
        res = res.filter(x => !vals.includes(x.value));
      }
      return res;
    },
    // val -> 数据的值    code -> 数据字典的 code 码
    createDlrDictText(val, code) {
      let res = '';
      if (!val) return res;
      if (_.isObject(this.dlrDict) && Array.isArray(this.dlrDict[code])) {
        const target = this.dlrDict[code].find(x => x.codeId == val);
        res = target ? target.codeCnDesc || '' : val;
      }
      return res;
    },
    // code -> 数据字典的 code 码
    // vals -> 需要过滤数据字典项的值
    createDictList(code, vals = []) {
      vals = Array.isArray(vals) ? vals : [vals];
      let res = [];
      if (_.isObject(this.dict) && Array.isArray(this.dict[code])) {
        res = this.dict[code].map(x => ({
          text: x.codeCnDesc,
          value: x.codeId
        }));
        res = res.filter(x => !vals.includes(x.value));
      }
      return res;
    },
    // val -> 数据的值    code -> 数据字典的 code 码
    createDictText(val, code) {
      let res = '';
      if (!val) return res;
      if (_.isObject(this.dict) && Array.isArray(this.dict[code])) {
        const target = this.dict[code].find(x => x.codeId == val);
        res = target ? target.codeCnDesc || '' : val;
      }
      return res;
    },
    creatActionPermiss(menuId) {
      let res = [];
      if (_.isObject(this.action) && Array.isArray(this.action[menuId])) {
        res = this.action[menuId];
      }
      return res;
    },
    createRegionList() {
      let res = [];
      if (_.isObject(this.region)) {
        for (let i in this.region) {
          res.push({
            text: this.region[i].regionName,
            value: this.region[i].regionId,
            children: this.region[i].children
          });
        }
      }
      return res;
    },
    // 获取省列表
    createRegionsList() {
      let res = [];
      if (_.isObject(this.region)) {
        for (let i in this.region) {
          res.push({
            text: this.region[i].regionName,
            value: this.region[i].regionId
          });
        }
      }
      return res;
    },
    // 获取市列表
    createcityList(id) {
      let res = [];
      if (_.isObject(this.region)) {
        for (let i in this.region) {
          if (i == id) {
            let arr = this.region[i].children;
            for (let j in this.region[i].children) {
              res.push({
                text: this.region[i].children[j].regionName,
                value: this.region[i].children[j].regionId
              });
            }
          }
        }
      }
      return res;
    },
    // 获取区县列表
    district(id) {
      let res = [];
      if (_.isObject(this.region)) {
        for (let i in this.region) {
          let arr = this.region[i].children;
          for (let j in this.region[i].children) {
            if (j == id) {
              for (let k in this.region[i].children[j].children) {
                res.push({
                  text: this.region[i].children[j].children[k].regionName,
                  value: this.region[i].children[j].children[k].regionId
                });
              }
            }
          }
        }
      }
      return res;
    },
    createDictRegion() {
      return this.deepFunc(this.region);
    },
    // 递归构建省市区数据
    deepFunc(data) {
      const res = [];
      for (let key in data) {
        const target = {
          value: data[key]['regionCode'],
          text: data[key]['regionName']
        };
        if (_.isObject(data[key].children) && Object.keys(data[key].children).length) {
          target.children = this.deepFunc(data[key].children);
        }
        res.push(target);
      }
      return res;
    },
    //获取字典列表，排除某些代码
    //codes必须为数组  例如：this.createDictListRemoveCodes('2503',['25031004'])
    //2019-09-23 wangyan
    createDictListRemoveCodes(code, codes) {
      let res = [];
      if (_.isObject(this.dict) && Array.isArray(this.dict[code])) {
        let codeDict = Object.assign([], this.dict[code]);
        if (Array.isArray(codes)) {
          //如果参数为数组
          for (let i in codes) {
            //循环排除数组
            for (let j in codeDict) {
              //循环数据数组
              if (codeDict[j].codeId == codes[i]) {
                //如果相同，则删除
                codeDict.splice(j, 1);
              }
            }
          }
        }
        res = codeDict.map(x => ({
          text: x.codeCnDesc,
          value: x.codeId
        }));
      }
      return res;
    },
    // 2020-12-3 qjh 过滤筛选
    filterDictList(dictArr, vals = []) {
      vals = Array.isArray(vals) ? vals : [vals];
      let res = [];
      if (Array.isArray(dictArr)) {
        res = dictArr.map(x => ({
          text: x.codeCnDesc,
          value: x.codeId
        }));
        res = res.filter(x => !vals.includes(x.value));
      }
      return res;
    },
    // 2020-12-3 qjh 通过接口获取字段翻译
    transDictText(val, dirtArr) {
      let res = '';
      if (!val) return res;
      if (Array.isArray(dirtArr)) {
        const target = dirtArr.find(x => x.codeId == val);
        res = target ? target.codeCnDesc || '' : val;
      }
      return res;
    }
  }
};
